// pages/person/withdrawRecord/withdrawRecord.js

const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
var currentIndex = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cellsOne: [],
    cellsTwo: [],
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
        this.requestList();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
  //标签切换
  onChange(event) {
    currentIndex = event.detail.index;
    this.setData({
      page: 1
    });
    console.log('============='+currentIndex);
    this.requestList();
  },
  //请求列表
  requestList() {
    var exchangeTypeStr = "收入";
    if (currentIndex == 1) {
      exchangeTypeStr = "支出";
    }
    let obj = {
      method: 'GET',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId + '/exchanges',
      showLoading: true,
      data: {
        page: this.data.page,
        size: 20,
        exchangeType: exchangeTypeStr
      }
    }
    httpUtils.request(obj).then(res => {
      if (currentIndex == 1) {
        if (this.data.page == 1) {
          this.setData({
            cellsTwo: []
          });
        }
        const {
          cellsTwo = []
        } = this.data;
        res.data.userExchangeInfos.forEach(function (item, index) {
          cellsTwo.push(item);
        });
        this.setData({
          cellsTwo
        });
      } else {
        if (this.data.page == 1) {
          this.setData({
            cellsOne: []
          });
        }
        const {
          cellsOne = []
        } = this.data;
        res.data.userExchangeInfos.forEach(function (item, index) {
          cellsOne.push(item);
        });
        this.setData({
          cellsOne
        });
      }
      
    }).catch(err => {
      console.log(err)
    });
  }
})